package cc.yihy.util.tree.bean;

import java.util.*;

/**
 * @author Yihy
 * @version V1.0
 * @Package cc.yihy.util.bean
 * @Description:
 * @date 2017/6/8 17:25
 */
public class Tree<K, V> {

    private Map<K, TreeNode<K, V>> result = new HashMap();

    private Set<TreeNode<K, V>> treeNodeTopSet = new HashSet<>();


    public Tree(Map<K, TreeNode<K, V>> result, Set<TreeNode<K, V>> treeNodeTopSet) {
        this.result = result;
        this.treeNodeTopSet = treeNodeTopSet;
    }

    /**
     * 获取一级栏目
     *
     * @return
     */
    public Set<TreeNode<K, V>> getTreeTop() {
        return this.treeNodeTopSet;
    }

    /**
     * 使用key 获取类型
     *
     * @param key
     * @return
     */
    public TreeNode<K, V> getCurrentTree(Object key) {
        return result.get(key);
    }

    public V getCurrent(Object key) {
        return result.get(key) != null ? result.get(key).getBean() : null;
    }

    public V getCurrentParent(Object key) {
        TreeNode<K, V> tTreeNode = result.get(key);
        return tTreeNode != null ? (tTreeNode.getParent() != null ? tTreeNode.getParent().getBean() : null) : null;
    }

    public List<V> getCurrentChildren(Object key) {

        if (result.get(key) == null) return null;
        List<TreeNode<K, V>> children = result.get(key).getChildren();
        List<V> tmp = new ArrayList<>(children.size());
        for (TreeNode<K, V> child : children) {
            tmp.add(child.getBean());
        }
        return tmp;
    }
}
